package br.pucrs.lognplay.faces.bean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

import br.pucrs.lognplay.entity.Client;
import br.pucrs.lognplay.entity.DynamicView;
import br.pucrs.lognplay.entity.StaticView;
import br.pucrs.lognplay.entity.Tag;
import br.pucrs.lognplay.service.SearchService;
import br.pucrs.lognplay.service.StaticViewManagementService;
import br.pucrs.lognplay.service.TagManagementService;
import br.pucrs.lognplay.vo.Log;
import br.pucrs.lognplay.vo.Log.Severity;

@ManagedBean
@ViewScoped
public class SearchBean extends Bean {

	private String selectedView;

	private List<Tag> selectedTags;

	private Date from;

	private Date to;

	private String message;

	private List<String> selectedSeverities;

	private List<Log> logs;

	private Log selectedLog;

	@EJB
	private StaticViewManagementService staticViewManagementService;

	@EJB
	private TagManagementService tagManagementService;

	@EJB
	private SearchService searchService;

	public String getSelectedView() {
		return selectedView;
	}

	public void setSelectedView(String selectedView) {
		this.selectedView = selectedView;
	}

	public List<Tag> getSelectedTags() {
		return selectedTags;
	}

	public void setSelectedTags(List<Tag> selectedTags) {
		this.selectedTags = selectedTags;
	}

	public Date getFrom() {
		return from;
	}

	public void setFrom(Date from) {
		this.from = from;
	}

	public Date getTo() {
		return to;
	}

	public void setTo(Date to) {
		this.to = to;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<String> getSelectedSeverities() {
		return selectedSeverities;
	}

	public void setSelectedSeverities(List<String> selectedSeverities) {
		this.selectedSeverities = selectedSeverities;
	}

	public List<Log> getLogs() {
		return logs;
	}

	public void setLogs(List<Log> logs) {
		this.logs = logs;
	}

	public Log getSelectedLog() {
		return selectedLog;
	}

	public void setSelectedLog(Log selectedLog) {
		System.out.println(selectedLog);
		this.selectedLog = selectedLog;
	}

	public List<SelectItem> getViews() {
		try {
			List<SelectItem> views = new ArrayList<>();
			Client loggedUserClient = getLoggedUser().getClient();

			DynamicView dynamicView = loggedUserClient.getDynamicView();
			SelectItem dynamicViewSelectItem = new SelectItem(dynamicView.getId(), dynamicView.getName());
			views.add(dynamicViewSelectItem);

			List<StaticView> staticViews = staticViewManagementService.listStaticViewsByClientId(loggedUserClient.getId());

			for (StaticView staticView : staticViews) {
				SelectItem staticViewSelectItem = new SelectItem(staticView.getId(), staticView.getName());
				views.add(staticViewSelectItem);
			}

			return views;
		} catch (Exception e) {
			throw new BeanException("Unexpected error", e);
		}
	}

	public List<Tag> getTags() {
		try {
			Client loggedUserClient = getLoggedUser().getClient();
			return tagManagementService.listTagsByClientId(loggedUserClient.getId());
		} catch (Exception e) {
			throw new BeanException("Unexpected error", e);
		}
	}

	public List<SelectItem> getSeverities() {
		List<SelectItem> severities = new ArrayList<>();

		for (Severity severity : Severity.values()) {
			SelectItem severitySelectItem = new SelectItem(severity.ordinal(), severity.getLabel());
			severities.add(severitySelectItem);
		}

		return severities;
	}

	public String search() {
		logs = searchService.listLogByCriteria(selectedView, selectedTags, from, to, message, selectedSeverities);
		return null;
	}
}
